//
//  RichTextTestController.m
//  TestDemo
//
//  Created by fengxijun on 4/21/16.
//  Copyright © 2016 goappbox. All rights reserved.
//

#import "RichTextTestController.h"
#import "FMRichTextView.h"
#import "MACRO.h"

#import "TAttachement.h"
#import "UITextView+YMRichText.h"

#import "YMLabel.h"
#import "UIImage+PHA.h"
#import "ImageCase.h"
#import "MACRO.h"

#define metamacro_concat(A, B) \
metamacro_concat_(A, B)
#define metamacro_concat_(A, B) A ## B

#define metamacro_exprify(...) \
((__VA_ARGS__), true)

@interface RichTextTestController ()
//<
//    UITextViewDelegate,
////    YMLabelDelegate
//>
//property_strong(NSString, test1)
@property (nonatomic, strong) NSString *test1;
@property (nonatomic, assign) NSInteger tes;
@end

@implementation RichTextTestController
{
    UITextView *textView;
}

def_property_strong(NSString, test1)


- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.test1 = @"Hello";
//    NSLog(@"%@",self.test1);
    
    
//    [self testImageCase];
//    UIImage *image = [UIImage imageNamed:@"example1.jpg"];
//    UIImage *gray = [[image scaleToSize:CGSizeMake(8.f, 8.f)] grayImage64];
//    UIImageView *v1 = [[UIImageView alloc] initWithImage:gray];
//    [v1 setFrame:CGRectMake(30.f, 80.f, 8.f, 8.f)];
//    [self.view addSubview:v1];
////
//    UIImage *image1 = [[[UIImage imageNamed:@"example1.jpg"] scaleToSize:CGSizeMake(8.f, 8.f)] imageContentData];
////    UIImage *gray1 = [[image1 scaleToSize:CGSizeMake(8.f, 8.f)] grayImage64];
//    UIImageView *v = [[UIImageView alloc] initWithImage:image1];
//    [v setFrame:CGRectMake(30.f, 200.f, 8.f, 8.f)];
//    [self.view addSubview:v];
    
//    NSArray *imageNames = @[@"example1.jpg",@"example2.jpg",@"example3.jpg",@"example4.jpg",@"example5.jpg",@"example6.jpg"];
//    
//    NSMutableArray *hashCodes = [[NSMutableArray alloc] init];
//    for (NSInteger i = 0; i < [imageNames count]; i++) {
//        NSString *name = [imageNames objectAtIndex:i];
//        UIImage *image = [UIImage imageNamed:name];
//        NSString *hashCode = [image imageFingerPrint];
//        [hashCodes addObject:hashCode];
//    }
//    
//    NSString *originHashCode = [[UIImage imageNamed:@"source.jpg"] imageFingerPrint];
//    
//    for (NSInteger i = 0; i < [imageNames count]; i++)
//    {
//        NSString *hashCode = [hashCodes objectAtIndex:i];
//        PHAHammingDistanceType pha = [ImageHash checkImagePHA:originHashCode destion:hashCode];
//        switch (pha) {
//            case PHAHammingDistanceWithSame:
//            {
//                NSLog(@"%@ : 相同",imageNames[i]);
//            }
//                break;
//            case PHAHammingDistanceWithMore:
//            {
//                NSLog(@"%@ : 非常相似",imageNames[i]);
//            }
//                break;
//            case PHAHammingDistanceWithLittle:
//            {
//                NSLog(@"%@ : 有点相似",imageNames[i]);
//            }
//                break;
//            case PHAHammingDistanceWithDifferent:
//            {
//                NSLog(@"%@ : 完全不同",imageNames[i]);
//            }
//                break;
//            default:
//                break;
//        }
//    }
    
    
//    NSArray *imageNames = @[@"example1.jpg"];
//    
////    for (NSString *name in imageNames) {
//////        UIImage *image = [UIImage imageNamed:name];
//////        NSString *hashCode = [image imageFingerPrint];
//////        NSLog(@"hashCode : %@",hashCode);
////        
////        UIImage *image = [[UIImage imageNamed:name] scaleToSize:CGSizeMake(8.f, 8.f)];
////        for (int i = 0; i < 5; i++) {
////            NSData *data = [image imageContentData];
////            NSLog(@"name :  %@ : length -> %lu : %@", name,(unsigned long)[data length],data);
////        }
////    }
//    
//    for (NSString *name in imageNames) {
//        UIImage *image = [UIImage imageNamed:name];
//        NSString *hashCode = [image imageFingerPrint];
//        NSLog(@"hashCode : %@",hashCode);
//        
//    }
//    fffffff38b1fffff
//    [self testTextViewUrlAndAttachement];
    
//    [self testRichTextLabel];
}

- (void) testRichTextLabel
{
    YMLabel *txtLabel = [[YMLabel alloc] initWithFrame:CGRectMake(10.f, 100.f, 300.f, 400.f)];
    txtLabel.delegate = self;
    [self.view addSubview:txtLabel];
    txtLabel.lineBreakMode = NSLineBreakByWordWrapping;
    txtLabel.backgroundColor = [UIColor yellowColor];
    txtLabel.cornerRadius = 3.f;
    txtLabel.lineFragmentPadding = 5.f;
    txtLabel.font = [UIFont systemFontOfSize:15.f];
    
    txtLabel.emojiTextMapper = @{
                                 @"[thumbIcon]": @"thumbIcon"
                                 };

    txtLabel.hyperlinkMapper = @{
                                 @"@百度": @"https://www.baidu.com",
                                 @"@腾讯": @"https://www.qq.com",
                                 @"@谷歌": @"https://www.google.com",
                                 @"@脸书": @"https://www.facebook.com",
                                 };
    
    txtLabel.text = @"很久很久以前[thumbIcon]，在一个群里，生活着@百度、@腾讯这样的居民，后来，一个[thumbIcon]叫做@谷歌的人入侵了这个村庄，他的同伙@脸书让整个群里变得淫荡无比。从此[thumbIcon]，迎来了污妖王的时代。污妖王，我当定了！[thumbIcon]";
    
    txtLabel.textContainerInsets = UIEdgeInsetsMake(10.f, 10.f, 10.f, 10.f);
    [txtLabel sizeToFit];
}

- (void) testTextViewUrlAndAttachement
{
    textView = [[UITextView alloc] initWithFrame:CGRectMake(0.f, 100.f, 320.f, 400.f)];
    textView.backgroundColor = [UIColor redColor];
    textView.font = [UIFont systemFontOfSize:30.f];
    [textView swizzing_setTextContainerInset:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];
    textView.dataDetectorTypes = UIDataDetectorTypeLink;
    
    textView.editable = NO;
    textView.delegate = self;
    [self.view addSubview:textView];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"Hello!中国风 测试。"];
    [attrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"Welcome" attributes:@{NSForegroundColorAttributeName : [UIColor blueColor],NSLinkAttributeName : [NSURL URLWithString:@"apptestddd"]}]];
    
    
    TAttachement *att = [[TAttachement alloc] initWithContentFile:[[NSBundle mainBundle] pathForResource:@"thumbIcon" ofType:@"png"]];
    [attrStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:att] atIndex:attrStr.string.length];
    
//    TAttachement *att1 = [[TAttachement alloc] initWithContentFile:[[NSBundle mainBundle] pathForResource:@"bianjitupian" ofType:@"png"]];
//    [attrStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:att1] atIndex:attrStr.string.length];
    
//    TAttachement *att2 = [[TAttachement alloc] initWithContentFile:[[NSBundle mainBundle] pathForResource:@"thumbIcon" ofType:@"png"]];
//    [attrStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:att2] atIndex:attrStr.string.length];
    
    //    [NSURL URLWithString:@"http://cc.cocimg.com/api/uploads/20160509/1462759590490540.jpg"]
    //    TAttachement *attUrl = [[TAttachement alloc] initWithUrl:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"thumbIcon" ofType:@"png"]]];
    //
    //    [attrStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:attUrl] atIndex:attrStr.string.length];
    
    textView.attributedText = attrStr;
    //    [textView sizeToFit];
    
    //    CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, MAXFLOAT)];
    //    NSLog(@"size : %@",NSStringFromCGSize(size));
}

//- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
//{
//    return YES;
//}
//
//- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
//{
//    [self doAction:URL];
//    return NO;
//}
//
//-(void)doAction:(NSURL *)url
//{
//    NSLog(@"url : %@",url);
//}

//- (void)ymLabel:(YMLabel *)label shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
//{
//    NSLog(@"tapImage : %@",textAttachment);
//}
//
//- (void)ymLabel:(YMLabel *)label shouldInteractWithStringAttributes:(NSDictionary *)attributes inRange:(NSRange)characterRange
//{
//    NSLog(@"attributes : %@",attributes);
//}

//- (void)textKit
//{
//    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
//}

- (void)testImageCase
{
    ImageCase *imageCase = [[ImageCase alloc] initWithFrame:CGRectMake(10.f, 74.f, self.view.frame.size.width - 20.f, self.view.frame.size.height - 84.f)];
    imageCase.backgroundColor = [UIColor blackColor];
    [self.view addSubview:imageCase];
}

@end
